package com.easondongh.blog.controller;

import com.easondongh.blog.enums.ResultEnum;
import com.easondongh.blog.enums.StateEnum;
import com.easondongh.blog.pojo.Admin;
import com.easondongh.blog.service.AdminService;
import com.easondongh.blog.token.UsernamePasswordToken;
import com.easondongh.blog.utils.Result;
import com.easondongh.blog.utils.ShiroUtils;
import com.easondongh.blog.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author EasonDongH
 * @date 2020/3/19 16:52
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录，成功后返回Token
     * @param admin
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<Object> login(@RequestBody Admin admin) {
        if(admin == null || StringUtils.isBlank(admin.getUsername()) || StringUtils.isBlank(admin.getPassword())) {
            return new Result<>(ResultEnum.INVALID_USERNAME_OR_PASSWORD);
        }
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken authenticationToken = new UsernamePasswordToken(admin.getUsername(), admin.getPassword(), StateEnum.ADMIN.getCode());
        try {
            subject.login(authenticationToken);
        } catch (Exception e) {
            log.error("[管理员登录异常] ", e);
            return new Result<>(ResultEnum.INVALID_USERNAME_OR_PASSWORD);
        }
        Serializable sessionId = subject.getSession().getId();
        Map<String,Object> returnMap = new HashMap<>(2);
        returnMap.put("token", sessionId);
        return new Result<>(returnMap);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result<Admin> getLoginInfo() {
        Admin loginAdmin = (Admin) ShiroUtils.getLoginUser();
        loginAdmin.setPassword("");
        return new Result<>(loginAdmin);
    }

    /**
     * 获取管理员
     * @return
     */
    @RequestMapping(value = "/getAdmin", method = RequestMethod.GET)
    public Result<Admin> getAdmin() {
        Admin admin = adminService.getAdmin();
        return new Result<>(admin);
    }

    /**
     * 更新个人信息
     * @param admin
     * @return
     */
    @RequestMapping(value = "/updateInfo", method = RequestMethod.PUT)
    public Result<Object> updateInfo(@RequestBody Admin admin){
        this.adminService.updateInfo(admin);
        return new Result<>(ResultEnum.SUCCESS);
    }

    /**
     * 更新密码
     * @param admin
     * @return
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
    public Result<Object> updatePassword(@RequestBody Admin admin) {
        this.adminService.updatePassword(admin);
        return new Result<>(ResultEnum.UPDATE_PASSWORD_SUCCESS);
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public Result<Object> logout() {
        Subject lvSubject=SecurityUtils.getSubject();
        lvSubject.logout();
        return new Result<>(ResultEnum.SUCCESS);
    }
}
